package cn.neusoft.controller;

import cn.neusoft.domain.Commodity;
import cn.neusoft.domain.MvpSubstation;
import cn.neusoft.service.MvpSubstationService;
import cn.neusoft.service.MvpSupplierService;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController // @Controller + @ResponseBody
@RequestMapping(value = "/mvpSubstation",produces = "application/json;charset=utf-8")
//接口文档的注解
@Api(value = "分站结算信息模块的API",description="分站结算功能")
public class MvpSubstationController {
  @Autowired
  MvpSupplierService mvpSupplierService;

  @Autowired
  MvpSubstationService mvpSubstationService;

  @PostMapping("/mvp")
  @ApiOperation(value = "获取结算信息")
  public ResultBean findMvpInfo(@RequestBody MvpSubstation mvpSubstation){
    String commodity_id=mvpSubstation.getCommodity_id();
    String measure=mvpSupplierService.findCommodity(commodity_id).getMeasure();
    String commodity_name=mvpSupplierService.findCommodity(commodity_id).getCommodity_name();
    Commodity commodity =  mvpSupplierService.findCommodity(commodity_id);
    Double price=commodity.getOriginal_price()*commodity.getDiscount();
    Long delivery_amount= mvpSubstationService.findDeliveryAmount(mvpSubstation);
    if(delivery_amount==null){
      return ResultBean.nonsupply();
    }
    Long return_amount=mvpSubstationService.findReturnAmount(mvpSubstation);
    if (return_amount==null){
      return_amount= Long.valueOf(0);}
    BigDecimal post_fee=mvpSubstationService.findPostFee(mvpSubstation);
    BigDecimal delivery_fee= BigDecimal.valueOf(delivery_amount*price);
    BigDecimal return_fee=BigDecimal.valueOf(return_amount*price);
    BigDecimal total_fee=delivery_fee.subtract(return_fee).add(post_fee);
    mvpSubstation.setCommodity_name(commodity_name);
    mvpSubstation.setMeasure(measure);
    mvpSubstation.setDelivery_amount(delivery_amount);
    mvpSubstation.setReturn_amount(return_amount);
    mvpSubstation.setDelivery_fee(delivery_fee);
    mvpSubstation.setReturn_fee(return_fee);
    mvpSubstation.setTotal_fee(total_fee);
    return ResultBean.success(mvpSubstation);
  }
}
